package com.zhiyou100.action;

import cn.hutool.core.util.ObjectUtil;
import com.zhiyou100.entity.Student;
import com.zhiyou100.service.StudentService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author yang
 * @className StudentAction
 * @packageName com.zhiyou100.action
 * @Description TODO 学生 action
 * @date 2020-08-22 15:36
 */
@Controller
@RequestMapping("/student")
public class StudentAction {

    private StudentService studentDao;

    @Autowired
    public void setStudentDao(StudentService studentDao) {
        this.studentDao = studentDao;
    }

    @RequestMapping("/login.action")
    public String login(Integer sid, String spwd, Model model) {
        final Student student = studentDao.selectBySidAndSpwd(sid, spwd);

        if (ObjectUtil.isNull(student)) {
            // 为空，没有此人，跳转到登陆界面，设置提示信息
            model.addAttribute("message", "登陆失败，请重新输入！！");
            return "index";
        } else {
            // 登陆成功,放入 request中
            model.addAttribute("user", studentDao.getOneBySid(student.getSid()));
            return "jsp/main";
        }
    }


    @SneakyThrows
    @RequestMapping("/register.action")
    public String register(String sname, String spwd, @RequestParam("sphoto") CommonsMultipartFile file, Date sbirthday, HttpServletRequest request) {

        // 上传位置保存的位置
        final String path = request.getServletContext().getRealPath("/files");
        final File realPath = new File(path);

        // 防止文件名重复，加上毫秒值
        final String filename =
                System.currentTimeMillis() + file.getOriginalFilename();

        // 最终的地址
        final File finalPath = new File(realPath, filename);

        // 通过 通过CommonsMultipartFile的方法直接写文件
        file.transferTo(finalPath);


        final Student student = new Student(sname, spwd, filename, sbirthday);
        studentDao.addOne(student);


        String message = "";

        request.setAttribute("message", message);

        return "jsp/register";
    }

    /**
     * 自定义一个 日期属性编辑器
     *
     * @param request HttpServletRequest
     * @param binder  binder
     */
    @InitBinder
    public void myDateBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        binder.registerCustomEditor(Date.class, new CustomDateEditor(simpleDateFormat, true));
    }


}
